package ckw.database.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import core.communication.electionConfiguration.CVoter;
import core.database.AbstractEntity;
import core.database.SexType;

@Entity
@Table(name = "voter")
public class Voter extends AbstractEntity {
	@Column(name = "name", length = 45)
	private String name;

	@Column(name = "surname", length = 45)
	private String surname;

	@Column(name = "address")
	private String address;

	@Column(name = "sex", nullable = false)
	@Enumerated(EnumType.STRING)
	private SexType sex;

	@Column(name = "identificationField", nullable = false, unique = true, length = 45)
	private String identificationField;

	@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.DETACH })
	@JoinColumn(name = "node", insertable = true, updatable = true)
	private Node node;

	@OneToMany(mappedBy = "voter", fetch = FetchType.LAZY)
	private List<ElectionResidentVoter> elections = new ArrayList<ElectionResidentVoter>();

	@OneToMany(mappedBy = "voter", fetch = FetchType.LAZY)
	private List<ElectionNonResidentVoter> nonResidentElections = new ArrayList<ElectionNonResidentVoter>();

	public Voter() {
		super();
	}

	public Voter(String name, String surname, String address, SexType sex, String identificationField, Node node) {
		super();
		this.name = name;
		this.surname = surname;
		this.address = address;
		this.sex = sex;
		this.identificationField = identificationField;
		setNode(node);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public SexType getSex() {
		return sex;
	}

	public void setSex(SexType sex) {
		this.sex = sex;
	}

	public String getIdentificationField() {
		return identificationField;
	}

	public void setIdentificationField(String identificationField) {
		this.identificationField = identificationField;
	}

	public Node getNode() {
		return node;
	}

	public void setNode(Node node) {
		this.node = node;
		node.getVoters().add(this);
	}

	public List<ElectionResidentVoter> getElections() {
		return elections;
	}

	public void setElections(List<ElectionResidentVoter> elections) {
		this.elections = elections;
	}

	public List<ElectionNonResidentVoter> getNonResidentElections() {
		return nonResidentElections;
	}

	public void setNonResidentElections(List<ElectionNonResidentVoter> nonResidentElections) {
		this.nonResidentElections = nonResidentElections;
	}

	public CVoter toCVoter() {
		return new CVoter(name, surname, address, sex, identificationField);
	}

	public void removeFromNode() {
		this.node.getVoters().remove(this);
		this.node = null;
	}
}
